import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/game_subcription_item.dart';
import 'package:flutter_syt/domain/games/subscription_manage_game_list.dart';
import 'package:flutter_syt/domain/games/subscription_manage_my.dart';
import 'package:flutter_syt/domain/games/subscription_manage_venue_list.dart';
import 'package:flutter_syt/domain/widgets/custom_dialog.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

import '../../global_easy_loading.dart';

@RoutePage(name: "SubScriptionManagePage")
class SubScriptionManagePage extends StatefulWidget {
  const SubScriptionManagePage();

  @override
  _SubScriptionManagePageState createState() => _SubScriptionManagePageState();
}

class _SubScriptionManagePageState extends State<SubScriptionManagePage> {
  ScrollController _scrollController = ScrollController();
  late GameSubcriptionManageData mySubscriptionData;

  late bool saveLoading = false;

  initData() async {
    mySubscriptionData = await API.shared.game.mySubscribe();
    // setState(() {
    //   mySubscriptionData = data;
    // });
  }

  saveData() async {
    if (saveLoading) {
      return;
    }
    try {
      saveLoading = true;
      bool saved =
          await API.shared.game.subscribe(GameSubcriptionManageData.model);
      // saveLoading = false;
      if (saved) {
        customShowSuccess(S.of(context).subscribe_notify_succeeded);
        saveLoading = false;
        Navigator.of(context).pop();
      }
    } catch (e) {
      await UtilsOther.showCustomShowError(e);
      // if (e is DioError) {
      //   customShowToast(e.message);
      // } else {
      //   customShowToast(S().network_error);
      // }
      saveLoading = false;
    }
  }

  @override
  void initState() {
    initData();
    super.initState();
  }

  Future<bool?> showConfirmDoalog() async {
    DialogEntity data = DialogEntity(
      S().Note,
      Container(
        padding: EdgeInsets.only(left: 10, right: 10, bottom: 16),
        child: Text(
          S().After_exit_toast,
          style: FontStyles.headerTextStyle_16.copyWith(
            color: ThemeColor.colorBlack80,
          ),
        ),
      ),
      () {
        Navigator.of(context).pop(false);
      },
      () async {
        Navigator.of(context).pop(true);
      },
      cancelText: S.of(context).cancel,
      okText: S.of(context).confirm,
      contentPadding: EdgeInsets.only(bottom: 0),
    );
    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return CustomDialog(data);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    var local =
        appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
    return WillPopScope(
      onWillPop: () async {
        if (mySubscriptionData.discipline.length ==
                GameSubcriptionManageData.model.discipline.length &&
            mySubscriptionData.venue.length ==
                GameSubcriptionManageData.model.venue.length) {
          return true;
        }
        bool confirm = await showConfirmDoalog() as bool;
        return confirm;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            S.of(context).subscriptions_manage_title,
            style: FontStyles.headerTextStyle_18.copyWith(
              fontWeight: FontWeight.w500,
            ),
            maxLines: 1,
          ),
          actions: [
            Container(
              width: local.contains('zh') ? 60 : 80,
              padding: EdgeInsets.symmetric(vertical: 12),
              margin: EdgeInsets.only(right: 15.0),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(padding: EdgeInsets.all(0)),
                child: Text(
                  S().Done,
                ),
                onPressed: ClickUtils.debounce(() {
                  saveData();
                }),
              ),
            ),
          ],
        ),
        body: Container(
          color: ThemeColor.bkColorF8F8F8,
          padding: EdgeInsets.all(16),
          // child: SubscriptionManageMy(),
          child: CustomScrollView(
            controller: _scrollController,
            clipBehavior: Clip.none,
            slivers: [
              SliverToBoxAdapter(
                child: SubscriptionManageMy(
                  scrollController: _scrollController,
                ),
              ),
              SliverToBoxAdapter(
                child: SubscriptionManageGameList(
                  scrollController: _scrollController,
                ),
              ),
              SliverToBoxAdapter(
                child: SubscriptionManageVenueList(
                  scrollController: _scrollController,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
